import "../../../scss/user_page/user_act.scss";

import $ from "jquery";
import Vue from "vue"
import page from "../../utils/oldcommon/page";
import com from "../../utils/common";
import moment from "moment";

import  "../../utils/remove_ad";
import "../../utils/oldcommon/layer";
import pageDeal from "../../utils/scrollPage"

$(function () {
  let page = {
    _allActMsg: [],
    _init: function () {
      // 页面初始获取值
      let _self = this;
      _self.beforeLoading();
    },
    _callback: function (data) {
      let _self = page;
      let allAct = data.data;
      _self._allActMsg = _self._analysisData(allAct);
      _self.beforeLoading();
    },
    beforeLoading: function () {
      let _self = this;
      let app = new Vue({
        el: "#app",
        data: {
          Navcurrent: 0,
          Allstatus: ["全部", "未开始", "进行中", "已结束"],
          Status: ["All", "NoStart", "Start", "End"],
          Allactmsg: _self._allActMsg,
          Id: '',
          Show: false,
        },
        methods: {
          // 查询活动
          SearchAct: function (index) {
            $("body").append('<div class="dropload-down"><div class="dropload-load"><span class="loading">数据加载中,请稍后</span></div></div>');
            // 需要将滚动距离设置为0,不然将会重复加载
            $(window).scrollTop(0);
            // 如果需要滚动加载,则让其从第1页开始
            pageDeal.pageSize = 1;
            // 默认滚动可以加载数据
            pageDeal.goahead = 1;
            // 现在就要让这家伙滚动起来
            let _app = this;
            this.Navcurrent = index;
            let obj = {
              data: {
                Status: _app.Status[index],
                page: 0,
                PageNum: 10,
              },
              url: '/userPage/getUser' + _app.Status[index],
              callback: function (data) {
                let _self = page;
                let allAct = data.data;
                _app.Allactmsg = _self._analysisData(allAct);
                if (_app.Allactmsg.length < 10) {
                  pageDeal.goahead = 0;
                  $(".dropload-down").html('<div class="dropload-load"><span class="loading">数据已全部加载完成</span></div>');
                  setTimeout(function () {
                    $('.dropload-down').remove();
                  }, 2000)
                  return
                }
                // 当数据大于一页时
                let scrollObj = {
                  Status: _app.Status[index],
                  url: '/userPage/getUser' + _app.Status[index],
                  callback: function (data) {
                    // 这是处理滚动带来的数据
                    let allAct = data.data;
                    let nowGetAct = _self._analysisData(allAct);
                    _app.Allactmsg = _app.Allactmsg.concat(nowGetAct);
                    setTimeout(function () {
                      $('.dropload-down').remove();
                    }, 2000)
                  }
                }
                pageDeal.startscroll(scrollObj);
              }
            }
            pageDeal.getData(obj);
          },
          getJoinerList:function (actid) {
            window.location = "/dist/activity/user_list.html?actid=" + actid
          },
          More: function (actId) {
            this.ActId = actId;
            this.Show = true;
          },
          // 删除活动
          DelAct: function (actId) {

            layer.open({
              content: '活动删除后数据无法恢复,是否删除',
              btn: ['确定', '取消'],
              yes: function (index) {
                layer.close(index);
                com.ajax({
                  url: '/userPage/delUser',
                  data: {
                    actId: actId
                  },
                  ok: function (data) {
                    console.log(data);
                    location.reload()
                  },
                  fail: function (data) {
                    console.log(data);
                  }
                })
              },
              no: function (index) {
                layer.close(index);
              },
            });
          },
          ShowQrcode: function (actId) {
            console.log(actId);
          },
          ClosePop: function () {
            this.Show = false;
          }
        },
      });
      app.SearchAct(0)  // 默认执行点击全部
    },
    // 解析获取数据
    _analysisData: function (data) {
      let acts = [];
      console.log(data);
      if (data.length == 0) {
        return []
      }
      for (let i = 0; i < data.length; i++) {
        let act = {};
        act.Title = data[i].Title;
        act.EndTime = moment(data[i].EndTime).format('YYYY-MM-DD HH:mm:ss');
        act.Catgory = data[i].Catgory;
        let actIconArr = act.Catgory.split("/");
        act.Href = "/dist/activity/" + act.Catgory + ".html";
        act.EditHref = "/dist/activity/" + act.Catgory + "_edit.html";
        act.Kind = data[i].Key;
        act.Type = data[i].Key;
        act.Domain = data[i].Domain;
        act.Id = data[i].Id;
        act.Icon = "/img/user_page/user_act/" + actIconArr[1] + "_icon.png";
        let startTime = data[i].StartTime;
        let endTime = data[i].EndTime;
        let now = moment();
        if (now.isBefore(startTime)) {
          act.Status = "noStart";
          act.StatusText = "未开始";
        }
        else if (now.isAfter(endTime)) {
          act.Status = "finish";
          act.StatusText = "已结束";
        } else if (now.isAfter(startTime) && now.isBefore(endTime)) {
          act.Status = "processing";
          act.StatusText = "进行中";
        }
        acts.push(act);
      }
      return acts
    }
  }
  page._init();
});